home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1320 / 1320.xpi / components / gmLogger.js < prev    next >
Encoding:
JavaScript  |  2010-01-22  |  3.0 KB  |  105 lines

  1. // Gmail Manager
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/gmanager/
  4.  
  5. function gmLogger()
  6. {
  7.   // Load the console service
  8.   this._console = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  9.   
  10.   // Load the preference branch observer
  11.   var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  12.   this._branch = prefService.getBranch("longfocus.gmanager.").QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  13.   this._branch.addObserver("", this, false);
  14.   
  15.   // Get the current debug preference value (silent)
  16.   this._debug = this._branch.getBoolPref("debug");
  17. }
  18. gmLogger.prototype = {
  19.   _console: null,
  20.   _branch: null,
  21.   _debug: false,
  22.   
  23.   log: function(aMsg)
  24.   {
  25.     // Check if debug is enabled
  26.     if (this._debug)
  27.     {
  28.       // Log the message to the console
  29.       this._console.logStringMessage("gmanager: " + aMsg);
  30.     }
  31.   },
  32.   
  33.   _toggle: function()
  34.   {
  35.     // Get the current debug preference value
  36.     this._debug = this._branch.getBoolPref("debug");
  37.     
  38.     // Display the logging status
  39.     this._console.logStringMessage("gmanager: " + "Logging has been " + (this._debug ? "enabled" : "disabled"));
  40.   },
  41.   
  42.   observe: function(aSubject, aTopic, aData)
  43.   {
  44.     if (aTopic == "nsPref:changed")
  45.     {
  46.       switch (aData)
  47.       {
  48.         case "debug":
  49.           // Toggle the logging status
  50.           this._toggle();
  51.           break;
  52.       }
  53.     }
  54.   },
  55.   
  56.   QueryInterface: function(iid)
  57.   {
  58.     if (iid.equals(Components.interfaces.gmILogger) ||
  59.         iid.equals(Components.interfaces.nsISupports))
  60.       return this;
  61.     throw Components.results.NS_ERROR_NO_INTERFACE;
  62.   }
  63. }
  64.  
  65. var myModule = {
  66.   firstTime: true,
  67.   
  68.   myCID: Components.ID("{07d9b512-8e83-418a-a540-0ec804b82195}"),
  69.   myDesc: "Debug Logger Service",
  70.   myProgID: "@longfocus.com/gmanager/logger;1",
  71.   myFactory: {
  72.     createInstance: function(outer, iid) {
  73.       if (outer != null)
  74.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  75.       
  76.       return (new gmLogger()).QueryInterface(iid);
  77.     }
  78.   },
  79.   
  80.   registerSelf: function(compMgr, fileSpec, location, type)
  81.   {
  82.     if (this.firstTime) {
  83.       this.firstTime = false;
  84.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  85.     }
  86.     
  87.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  88.     compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
  89.   },
  90.   
  91.   getClassObject: function(compMgr, cid, iid)
  92.   {
  93.     if (!cid.equals(this.myCID))
  94.       throw Components.results.NS_ERROR_NO_INTERFACE;
  95.     
  96.     if (!iid.equals(Components.interfaces.nsIFactory))
  97.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  98.     
  99.     return this.myFactory;
  100.   },
  101.   
  102.   canUnload: function(compMgr) { return true; }
  103. };
  104.  
  105. function NSGetModule(compMgr, fileSpec) { return myModule; }